package org.dromara.genealogy.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import org.dromara.genealogy.domain.bo.GeNameBo;
import org.dromara.genealogy.domain.vo.GeNameTranVo;
import org.dromara.genealogy.domain.vo.GeNameVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.genealogy.domain.vo.GeNameDraftVo;
import org.dromara.genealogy.domain.bo.GeNameDraftBo;
import org.dromara.genealogy.service.IGeNameDraftService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

import java.util.List;

/**
 * 族员信息-草稿
 *
 * @author qianjm
 * @date 2025-10-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/genealogy/nameDraft")
public class GeNameDraftController extends BaseController {

    private final IGeNameDraftService geNameDraftService;

    /**
     * 查询族员信息-草稿列表
     */
    @SaIgnore
    @GetMapping("/list")
    public R<Page<GeNameDraftVo>> list(GeNameDraftBo bo, PageQuery pageQuery) {
        return R.ok(geNameDraftService.queryPageList(bo, pageQuery));
    }

    /**
     * 获取世代名称详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("genealogy:name:query")
    @GetMapping("/{id}")
    public R<GeNameDraftVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(geNameDraftService.queryById(id));
    }

    /**
     * 获取族员草稿详细信息(字段翻译之后)
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/trans/{id}")
    public R<GeNameTranVo> getInfoTrans(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(geNameDraftService.queryTransById(id));
    }

    /**
     * 新增族员信息-草稿
     */
    @SaIgnore
    @Log(title = "族员信息-草稿", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GeNameDraftBo bo) {
        return toAjax(geNameDraftService.insertByBo(bo));
    }

    /**
     * 修改族员信息-草稿
     */
    @SaIgnore
    @Log(title = "族员信息-草稿", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GeNameDraftBo bo) {
        return toAjax(geNameDraftService.updateByBo(bo));
    }

    /**
     * 获取族员树列表
     */
    @SaCheckPermission("genealogy:name:query")
    @PostMapping("/genealogyNameTree")
    public R<List<Tree<Long>>> genealogyNameTree(GeNameDraftBo bo) {
        return R.ok(geNameDraftService.genealogyNameTreeList(bo));
    }
}
